package com.woniuxy.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: mayuhang  <br/>
 * Date: 2021/3/17:16:03  <br/>
 * Description:延迟队列配置类
 */
@Configuration
public class LazyExchangeConfig {
    public static final String LAZY_EXCHANGE="Ex.LazyExchange";
    public static final String LAZY_QUEUE="MQ.LazyQueue";
    public static final String LAZY_QUEUE1="MQ.LazyQueue1";
    public static final String LAZY_KEY="lazy.#";
    public static final String LAZY_KEY1="lazy1.#";
    @Bean
    public CustomExchange lazyExchange() {
        //第一种设置方法  设置延迟交换机配置
        Map<String, Object> pros = new HashMap<>();
          //设置交换机支持延迟消息推送 可以是topic也可以是direct，还能是fanout广播模式……
        pros.put("x-delayed-type", "topic");
        CustomExchange exchange = new CustomExchange(LAZY_EXCHANGE, "x-delayed-message",true, false,pros);
//        //第二种设置方法 这里有bug  如果直接就配置这个 就会报错 必须先用第一种 持久化交换机后就好了
//        exchange.setDelayed(true);
        return exchange;
    }
    @Bean
    public Queue lazyQueue(){
//        HashMap<String, Object> pros = new HashMap<>();
//        设置队列长度
//        pros.put("x-max-length",1);
//        return new Queue(LAZY_QUEUE,false,false,true,pros);
        return new Queue(LAZY_QUEUE,true);
    }
    @Bean
    public Queue lazyQueue1(){
//        HashMap<String, Object> pros = new HashMap<>();
//        设置队列长度
//        pros.put("x-max-length",1);
//        return new Queue(LAZY_QUEUE,false,false,true,pros);
        return new Queue(LAZY_QUEUE1,true);
    }
    @Bean
    public Binding lazyBinding(){

        return BindingBuilder.bind(lazyQueue()).to(lazyExchange()).with(LAZY_KEY).noargs();
    }
    @Bean
    public Binding lazyBinding1(){
        return BindingBuilder.bind(lazyQueue1()).to(lazyExchange()).with(LAZY_KEY1).noargs();
    }

}
